//查询排行榜
function rankFun(){
	//alert('----'+agentid);
	$.ajax({
		url: API_URL+'agentclub/gethotagent',
		type: 'GET',
		dataType: "JSON",
		data:'',
		async: false,
		success: function (obj){
			if (obj.status.code == 0) {
				var myData = obj.data;
				var Str = '';
				for(var i=0; i < myData.length; i++){
					if(myData[i].icon == null || myData[i].icon == ''){
						myData[i].icon ='../img/default.png';
					}
					 Str += '<dl>'+
								'<dd><img src=\"'+myData[i].icon+'\"><i class=""></i></dd>'+
								'<dt class="name">'+myData[i].name+'</dt>'+
								'<input type="hidden" value=\"'+myData[i].openid+'\" />'+
							'</dl>';
				}

				Str +='<dl class="more_detials">'+
						'<dd><img src="../img/icon-more.png"></dd><span class="line"></span>'+
						'<dt class="name">更多</dt>'+
						'<input type="hidden" value=\"'+openid+'\" />'+
					'</dl>';

					$('.flex_box').html(Str);
					//设置名次飘带样式
					$('.flex_box dl:first-child dd i').addClass('first');
					$('.flex_box dl:nth-child(2) dd i').addClass('second');
					$('.flex_box dl:nth-child(3) dd i').addClass('third');
					$('.flex_box dl:nth-child(4) dd i').addClass('fourth');
					$('.flex_box dl:last-child dd i').addClass('fiveth');
					//查看他的个人主页
					$('.person_page').on('click',function(){
						var userId = $(this).find('input').val();
						//alert(userId);
						if(userId==openid){
							 //跳转到个人主页
							 //alert('主页---'+agentid);
							 if (user_status == '1' || user_status == '2') {
								window.location = "club_list.html?openid="+openid+"&user_status="+user_status;
							 }else {
								if(confirm("您还没有认证，认证后将开启个人主页，现在去认证吧！")){
									window.location = '../validate.html?openid='+openid+'&state=2';
								}
							 }
						}else{
							//他人页
							window.location = "personal_page.html?openid="+userId+'&currentopenid='+openid+'&user_status='+user_status;
						}
					});
					
				}
		},
		error: function (data){
			console.log("服务器异常!");
		}
	});
}

//查询发表列表
var Str = '';
//类型
var typeValue='';
//全部页数
var totalPage= '';
//当前页数
var start=1;
var upTime,downTime;

var detialId = '';
var wellNum = '';
function detialFun(arg_url){
	//alert(arg_url);
	$.ajax({
		url: API_URL+arg_url,
		type: 'GET',
		dataType: "JSON",
		data:'',
		async: false,
		cache: false,
		success: function (obj){
			if (obj.status.code == 0) {
				var myData = obj.basedata.data;
				var talkStr = '';
				Str = '';
				if(myData.length > 0){
					if(obj.basedata.operation == "down"){
						downTime = myData[0].createtime_time;
					}else if(obj.basedata.operation == "up"){
						upTime = myData[myData.length-1].createtime_time;

					}else {
						upTime = myData[myData.length-1].createtime_time;
						downTime = myData[0].createtime_time;
					}
				}
				if(myData.length < 10){
					$("#pullUp").hide();
				}
				// Str += '<div class="list_content"><p><i class="queen"></i>讲师人气排行榜</p><div class="flex_box"></div></div>';
				for(var i=0; i < myData.length; i++){
					if(myData[i].agent_icon == null || myData[i].agent_icon == ''){

							myData[i].agent_icon = '../img/default.png';
						}
				wellNum = myData[i].praisecount;
				var welldone ='';
				if(myData[i].pa_state == 1){
					welldone = ' welldone';
				}
					
					 Str += '<div class="items_detials">'+
								'<div class="main_content">'+
					 				'<input type="hidden" value=\"'+myData[i].id+'\" id="itemsId" />'+
									'<input type="hidden" value=\"'+myData[i].agent_openid+'\" id="agent_openId" />'+
									'<div class="head_content">'+
										'<img src=\"'+myData[i].agent_icon+'\" alt="">'+
										'<h3>'+myData[i].agent_name;
						//alert(myData.agent_category);
						if(myData[i].agent_category == 2){
							Str += '<i class=""></i>';
						}else{
							Str += '<i class="brand_icon"></i>';
						}

					 Str += '</h3>'+
								'<p>'+myData[i].createtime_str+'</p>'+
								'<input type="hidden" value=\"'+myData[i].createtime+'\" />'+
							'</div>';
							if(myData[i].type == 'photo'){
								Str += '<p class="article clear">'+myData[i].content+'</p>';
								if(myData[i].photo!=null && myData[i].photo!=''){
									//alert('有图:'+myData[i].photo);
									Str += '<img class="detials_img" src=\"'+myData[i].photo+'\" alt=""/>';
								}
							}else if(myData[i].type == 'video'){
								Str += '<p class="article clear">'+myData[i].title+'</p>'+
									    '<iframe height="140" width="181" src=\"'+myData[i].url+'\" frameborder=0 allowfullscreen></iframe>'+
										'<ul class="radiolabel_content" >'+
											'<li style="display: inline-block;height: 14px;line-height: 14px;margin: 8px 4px 20px 0;background: #fff;border: none;"><span style="display: inline-block;padding: 3px 5px;text-align: center;border-radius: 3px;font-size: 9px;border: 1px solid #43aaf1;color: #43aaf1;margin: 8px 4px 12px 0;">'+myData[i].channelname+'</span></li>'+
											'<li style="display: inline-block;height: 14px;line-height: 14px;margin: 8px 4px 20px 0;background: #fff;border: none;"><span style="display: inline-block;padding: 3px 5px;text-align: center;border-radius: 3px;font-size: 9px;border: 1px solid #43aaf1;color: #43aaf1;margin: 8px 4px 12px 0;">'+myData[i].curriculumtypename+'</span></li>'+
											'<li style="display: inline-block;height: 14px;line-height: 14px;margin: 8px 4px 20px 0;background: #fff;border: none;"><span style="display: inline-block;padding: 3px 5px;text-align: center;border-radius: 3px;font-size: 9px;border: 1px solid #43aaf1;color: #43aaf1;margin: 8px 4px 12px 0;">'+myData[i].standardname+'</span></li>'+
										'</ul>';
									/*
											'<video height="140" width="181" poster="../img/banner_02.jpg" controls="controls" preload="none" webkit-playsinline="webkit-playsinline">'+
								                '<!-- <source src="'+myData[i].url+'" type="video/mp4" preload="auto"> -->'+
								             '</video>';
										*/
							}else{
								Str += '<p class="article clear">'+ myData[i].content+'</p>'
								Str += '<div class="pdf_content">' + 
									 	'<img class="pdf_img" src="../img/icon-pdf22.png" alt="">' + 
									 	'<h3>'+myData[i].title+'</h3>'+
									 	'</div>';
							}

								Str += '</div>'+
								'<div class="operate">'+
									'<span><i class="share"></i>'+myData[i].forwardcount+'</span>'+
									'<span><i class="say"></i>'+myData[i].commentcount+'</span>'+
									'<span class="well_btn'+welldone+'\"><i class="well"></i>'+myData[i].praisecount+'</span>'+
								'</div>';
								if(myData[i].commentcount>0){
									Str +='<p class="more_say" id="more_say"><i class="more_icon"></i>查看全部<em>'+myData[i].commentcount+'</em>条评论<span class="into"></span></p>'+
									'<div class="ourtalk_content">';
									for(var j=0; j<myData[i].agent_item.length; j++ ) {
										var agentItem = myData[i].agent_item;
										if(agentItem[j].agent_icon == null || agentItem[j].agent_icon == ''){
											agentItem[j].agent_icon = '../img/default.png';
										}
										var agent_time = new Date(agentItem[j].agent_time).format("yyyy-MM-dd hh:mm");//
										Str += '<div class="items clear">'+
													'<h3 class="talk_name">'+agentItem[j].agent_name+'：</h3>'+
													'<p>'+agentItem[j].agent_content+'</p>'+
												'</div></div>';
									}
								}
								Str +='</div>';
					}			
				}
		},
		error: function (data){
			console.log("服务器异常!");
		}
	});
}

$(function(){
		//发表图文
		var options={
		  url:API_URL+'curriculum/saveCurriculumFile?agentid='+agentid,
		  type:"post",
		  success:function(obj){
				if (obj.status.code == 0) {
					detialId = obj.basedata.id;
					window.location='status_detial.html?openid='+openid+'&detialId='+detialId+'&user_status='+user_status;
				}else{
					alert('服务错误');
				}
			}
		 };

	      $("#publishImg").submit(function() {
	          $(this).ajaxSubmit(options);
	          return false;
	      });

	      $("#push").click(function(){
			  if($("#content").val()==''){
				  alert('您还没有输入内容哦！');
				  return false;
			  }
			  $("#push").attr('disabled',true);
			  if($('#doc').val()==''){
				  //发单文本
				  $.ajax({
						url: API_URL+'curriculum/saveCurriculumFile',
						type: 'post',
						dataType: "JSON",
						data:{
							agentid:agentid,
							content:$('#content').val()
						},
						async: false,
						success: function (obj){
							if (obj.status.code == 0) {
								detialId = obj.basedata.id;
								window.location='status_detial.html?openid='+openid+'&detialId='+detialId+'&user_status='+user_status;
							}else{
								alert('服务错误');
							}
						},
						error: function (data){
							console.log("服务器异常!");
						}
					});
				  
			  }else{
				$("#publishImg").submit();
			  }
	      });

	//发表视频
    $('.video_btn').on('click',function(){
        window.location = 'newvideo.html?openid='+openid+'&agentid='+agentid+'&user_status='+user_status;
    });

    //发表课件
    $('.pdf_btn').on('click',function(){
        window.location = 'publish_lessonpdf.html?openid='+openid+'&agentid='+agentid+'&user_status='+user_status+'&type=pdf';
    });
    //发表案例
    $('.eg_btn').on('click',function(){
        window.location = 'publish_lessonpdf.html?openid='+openid+'&agentid='+agentid+'&user_status='+user_status+'&type=example';
    });
});

	function showclick(){
		$('.head_content').on('click',function(event){
			var agent_openId = $(this).siblings('#agent_openId').val();
			event.stopPropagation(); 
			//alert(agent_openId);
			window.location = "personal_page.html?openid="+agent_openId+'&currentopenid='+openid+'&user_status='+user_status;
		});

		$('.main_content').on('click',function(){
			detialId = $(this).children('#itemsId').val();
			//alert('detialId:'+detialId);
			var agent_openId = $(this).children('#agent_openId').val();
			window.location='status_detial.html?agentopenid='+agent_openId+'&openid='+openid+'&detialId='+detialId+'&user_status='+user_status;
		});
		$('.ourtalk_content').on('click',function(){
			$('.main_content').click();
		});
		$('.more_say').on('click',function(){
			//$('.main_content').click();
			detialId = $(this).siblings('.main_content').children('#itemsId').val();
			var agent_openId = $(this).siblings('.main_content').children('#agent_openId').val();
			//alert('detialId:'+detialId+'--->agent_openId:'+agent_openId);
			window.location='status_detial.html?agentopenid='+agent_openId+'&openid='+openid+'&detialId='+detialId+'&user_status='+user_status;
		
		});
		//转发
		$('.operate .share').on('click',function(){
			detialId = $(this).parents('.operate').siblings('.main_content').children('#itemsId').val();
			var agent_openId = $(this).siblings('.main_content').children('#agent_openId').val();
			//alert('detialId:'+detialId+'--->agent_openId:'+agent_openId);
			window.location='status_detial.html?agentopenid='+agent_openId+'&openid='+openid+'&detialId='+detialId+'&user_status='+user_status;
		});
		//评论
		$('.operate .say').on('click',function(){
			detialId = $(this).parents('.operate').siblings('.main_content').children('#itemsId').val();
			var agent_openId = $(this).siblings('.main_content').children('#agent_openId').val();
			//alert('detialId:'+detialId+'--->agent_openId:'+agent_openId);
			window.location='status_detial.html?agentopenid='+agent_openId+'&openid='+openid+'&detialId='+detialId+'&user_status='+user_status;
		});

		//赞
		$('.well_btn').on('click',function(){
			if ($(this).hasClass('welldone')) {
				cancelFun($(this));
			}else {
				wellDoneFun($(this));	
			}
		});
}

//点赞
function wellDoneFun(wellobj){
	detialId = $(wellobj).parents('.operate').siblings('.main_content').children('#itemsId').val();
	$.ajax({
		url: API_URL+'curriculum/praise?openid='+openid+'&curriculumid='+detialId,
		type: 'post',
		dataType: "JSON",
		data:'',
		async: false,
		success: function (obj){
			if (obj.status.code == 0) {
					$(wellobj).addClass('welldone');
					var numNew = parseInt($(wellobj).text())+ parseInt(1);
					$(wellobj).html('<i class="well"></i>'+numNew);
				}else if (obj.status.code == 1) {
					$(wellobj).addClass('welldone');
					var numNew = parseInt($(wellobj).text())+ parseInt(1);
					$(wellobj).html('<i class="well"></i>'+numNew);
				}
		},
		error: function (data){
			console.log("服务器异常!");
		}
	});
}
//取消赞
function cancelFun(wellobj){
	detialId = $(wellobj).parents('.operate').siblings('.main_content').children('#itemsId').val();
	$.ajax({
		url: API_URL+'curriculum/cancelpraise?openid='+openid+'&curriculumid='+detialId,
		type: 'post',
		dataType: "JSON",
		data:'',
		async: false,
		success: function (obj){
			if (obj.status.code == 0) {
					$(wellobj).removeClass('welldone');
					var numNew = parseInt($(wellobj).text()) - parseInt(1);
					$(wellobj).html('<i class="well"></i>'+numNew);
				}else if (obj.status.code == 1) {
					$(wellobj).removeClass('welldone');
					var numNew = parseInt($(wellobj).text()) - parseInt(1);
					$(wellobj).html('<i class="well"></i>'+numNew);
				}
		},
		error: function (data){
			console.log("服务器异常!");
		}
	});
}


//搜索限制条件选择
function checkLabel(){
	//定义一个数字number来表示上次选中的label的序号
	var checkNum=0;
	var labelObj = $('#check_items > label');
	//默认第一个有选中状态的是label  全部
	$(labelObj).eq(checkNum).addClass("checked");
	$(labelObj).eq(checkNum).find('i').addClass("checked");
	for(var i = 0; i < labelObj.length; i++){
		
		$(labelObj[i]).on('click',function(){
			//清除上次的label样式
			$(labelObj).eq(checkNum).removeClass("checked");
			$(labelObj).eq(checkNum).find('i').removeClass("checked");
			 if(event.target.type!="checkbox"){
		        $(this).find(":checkbox").click(function(){
		            event.stopPropagation();
		        })
		    }
		    //当前的label样式呈现选中样式
		    $(this).addClass("checked");
			$(this).find('i').addClass("checked");
			checkNum=$(this).index();
			//遮罩消失，去除sider_right的show样式
			$('.side_content').fadeOut(200, function() {
				$('.sider_right').removeClass('show');
			});
			typeValue=$(this).find("input").attr("name");
			alert(typeValue);
			$('.showitemdiv').html('');
			detialFun('agentclub/main?openid='+openid+'&type='+typeValue);
			$('.showitemdiv').html(Str);
			//alert($(this).find("input").attr("name"))
			//alert(checkNum)
			//detialFun('agentclub/main?openid='+openid+'&type='+);
		})
	}
}